<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加管理员</title>

    <link rel="stylesheet" href="/static/layui/css/layui.css">

</head>

<body style="padding: 10px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-inline">
                <select name="role" lay-verify="required">
                    {volist name='roles' id='role'}
                    <option value="{$role.id}">{$role.name}</option>
                    {/volist}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" autocomplete="off"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="realname" required lay-verify="required" placeholder="请输入姓名" autocomplete="off"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="checkbox" name="status" lay-filter="statusSwitch" lay-skin="switch" lay-text="启用|禁用"
                    value="0">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>



    <script src="/static/layui/layui.js"></script>
    <script>
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer;
            var $ = layui.jquery;
            var form = layui.form;

            // status状态开关选择事件
            form.on('switch(statusSwitch)', function (data) {
                if (data.elem.checked) {
                    data.value = 1;
                    $('#status').val(1);
                } else {
                    data.value = 0;
                    $('#status').val(0);
                }
            });

            //监听提交
            form.on('submit(formSubmit)', function (data) {
                $.post('/admin/admin/save', data.field, function (res) {
                    if (res.code == '200') {
                        layer.msg('添加成功', {
                            icon: 6,
                            time: 500
                        }, function () {
                            parent.window.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 1000
                        });
                    }
                }, 'json');

                return false;
            });
        });

    </script>
</body>

</html>